<template>
  <div class="mask" :style="{ height: maskHeight + 'px'}" @click.self="redirectClick">
    <Login
        v-if="!token"
    />
    <div class="user-info-show" v-if="token">
      <div class="title">{{ $t('login.login.title') }}</div>
      <div class="user-name">
        <span>{{ $t('login.info.userName') }}：</span>
        <span>{{ userInfo.lastName }}{{ userInfo.firstName }}</span>
      </div>
      <div class="divider-10"></div>
      <NuxtLink :to="$i18n.path('userInfo')" @click.native="redirectClick">{{ $t('login.info.userInfo') }}</NuxtLink>
      <div class="divider"></div>
      <div class="login-button" @click="handleLogoutButtonClick">{{ $t('login.info.logoutButtonText') }}</div>
    </div>
  </div>
</template>

<script>
import IntelInput from "../common/IntelInput";
import {getUserInfo, getUserInfoAuto, login} from "../../api/service";
import Login from "~/components/login/Login";

export default {
  name: "UserInfoMask",
  components: {
    IntelInput,
    Login
  },
  computed: {
    maskHeight() {
      if (process.client) {
        return document.body.clientHeight - 70
      }
    }
  },
  beforeMount() {
    // 获取原来的scrollTop 并将body的top修改为对应的值
    this.prevBodyStyle_scrollTop = document.body.scrollTop || document.documentElement.scrollTop
    this.prevBodyStyle_top = window.getComputedStyle(document.body, null).getPropertyValue('top')
    // document.body.style.top = `-${this.prevBodyStyle_scrollTop}px`
    document.body.style.top = `0px`
    // 获取原来body的position 为了解决iOS上光标漂移的问题 将position修改为fixed
    this.prevBodyStyle_position = window.getComputedStyle(document.body, null).getPropertyValue('position')
    document.body.style.position = 'fixed'
    // 为了避免width空值的情况
    this.prevBodyStyle_width = window.getComputedStyle(document.body, null).getPropertyValue('width')
    document.body.style.width = '100%'
  },
  beforeDestroy() {
    document.body.style.top = this.prevBodyStyle_top || '0px'
    document.body.style.position = this.prevBodyStyle_position
    document.body.style.width = '100%'
    document.body.scrollTop = document.documentElement.scrollTop = this.prevBodyStyle_scrollTop || 0
  },
  data() {
    return {
      loginName: '',
      password: '',
      token: null,
      errorShow: false,
      errorContent: '',
      userInfo: {
        lastName: '',
        firstName: ''
      }
    }
  },
  mounted () {
    this.token = localStorage.overnight_token
    this.getUserInfo()
  },
  methods: {
    handleLoginButtonClick () {
      const param = {
        loginEmail: this.loginName,
        loginPasswd: this.password
      }
      const that = this
      login(param).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.errorShow = false
          that.token = res.data
          localStorage.setItem('token', res.data)
          this.$emit('loginStateChange')
          that.getUserInfo()
          that.$router.push({
            path: '/'
          })
        } else {
          this.errorShow = true
          const errorInfo = res.message.split('$$')
          if (this.$i18n.locale === 'zh') {
            this.errorContent = errorInfo[0]
          } else {
            this.errorContent = errorInfo[1]
          }

        }
      })
    },
    getUserInfo () {
      getUserInfo().then(res => {
        console.log(res)
        if (res.code === 200) {
          this.userInfo = res.data
          localStorage.setItem('overnight_userInfo', JSON.stringify(res.data))
        }
      })
    },
    redirectClick () {
      this.$emit('redirect')
    },
    handleLogoutButtonClick () {
      localStorage.removeItem('overnight_token')
      localStorage.removeItem('accompany')
      localStorage.removeItem('travel')
      localStorage.removeItem('overnight_userInfo')
      this.$emit('redirect')
      this.$emit('loginStateChange')
      this.$store.commit('SET_LOGIN_STATE', false)
      this.$router.push({
        path: this.$i18n.path('logout')
      })
    },
    handleForgetLinkClick () {
      this.$emit('redirect')
      this.$router.push({
        path: this.$i18n.path('forgetPasswordP1')
      })
    }
  }
}
</script>

<style scoped>
.mask {
  width: 100%;
  background-color: rgba(0, 0, 0, .32);
  position: fixed;
  left: 0;
  top: 70px;
  z-index: 1000;
}

.user-info {
  width: 100%;
  background-color: white;
  display: flex;
  padding: 36px 32px 32px 32px;
  align-items: center;
  min-width: 1190px;
}

.login-box {
}


.form {
  display: flex;
  padding-top: 38px;
  padding-bottom: 16px;
}

.form .login-input {
  margin-right: 22px;
}

.login-button {
  width: 146px;
  height: 36px;
  color: white;
  font-size: 14px;
  text-align: center;
  line-height: 36px;
  background: #0068B5;
  cursor: pointer;
}

a {
  color: #0068B5;
  text-decoration: none;
}

.forget {
  padding-top: 16px;
  font-size: 14px;
  color: #999999;
}

.divider-v {
  width: 1px;
  height: 200px;
  border-left: 1px solid #D7D7D7;
  margin: 0 47px;
}

.register {
  font-size: 14px;
  height: 160px;
  color: #999999;
}

.user-info-show {
  width: 100%;
  background-color: white;
  display: flex;
  flex-direction: column;
  padding: 36px 32px 32px 32px;
  min-width: 1190px;
}
.user-info-show .title {
  padding-bottom: 40px;
}
.user-info-show .user-name {
  font-size: 12px;
  height: 18px;
  line-height: 18px;
  color: #333333;
}
.user-info-show a {
  font-size: 12px;
}
.divider-10 {
  height: 10px;
  width: 100%;
  display: block;
}
.divider {
  height: 20px;
  width: 100%;
  display: block;
}
</style>
